package com.alumni_circle.core.security;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 龙小虬
 * @since 2020-08-01 14:27
 *
 * 用户访问了本人无权限的返回类
 */
@Component
public class PermissionDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        Map<String,String> map = new HashMap<>();
        map.put("权限不足","403");
        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.setContentType("application/json;charset=utf-8");
        PrintWriter printWriter = httpServletResponse.getWriter();
        System.out.println(map.toString());
        JSON json = JSONUtil.parse(map);
        printWriter.write(json.toString());
    }
}
